#ifndef GRAPHICSVIEW_H
#define GRAPHICSVIEW_H

#include <QGraphicsView>
#include <QGraphicsScene>
#include <QMouseEvent>
#include "myshape.h"

enum class ShapeType {
    None,
    Point,
    Line,
    Rectangle,
    Circle
};

class GraphicsView : public QGraphicsView
{
    Q_OBJECT

public:
    explicit GraphicsView(QWidget *parent = nullptr);

    void setCurrentShapeType(ShapeType type);
    void setCurrentColor(const QColor &color);
    void deleteSelectedShape();
    void clearAllShapes();
    QPixmap getScenePixmap() const;
    void loadFromPixmap(const QPixmap& pixmap);

protected:
    void mousePressEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event) override;
    void mouseReleaseEvent(QMouseEvent *event) override;

private:
    QGraphicsScene *scene_;
    ShapeType currentShapeType_ = ShapeType::None;
    QColor currentColor_ = Qt::black;
    MyShape *currentShape_ = nullptr;
    MyShape *selectedShape_ = nullptr;
    QPointF startPos_;
    bool isDragging_ = false;

    void highlightNearbyPoints(const QPointF &pos);
    MyShape *findShapeAtPosition(const QPointF &pos);
    void createShape(const QPointF &pos);
    void updateShape(const QPointF &pos);
    void finishShape();
};

#endif // GRAPHICSVIEW_H
